//商场号码相关处理
(function ($) {
    $.fn.phone = function (options) {
        var settings = {
            url:setting.app + '/phone',
            input_mid:"input[id=mid]",
            input_fid:"input[id=fid]",
            input_bid:"input[id=bid]",
            input_name:"input[id=name]",
            input_phone:"input[id=phone]",
            input_remark:"input[id=remark]",
            input_add:"#phone_add",
            phone_do_add:'#phone_do_add',
            phone_list:"a.phone_list",
            effects:"normal"
        }
        if (options) {
            $.extend(settings, options);
        }
        // 执行赋值操作
        var mid, fid, bid, add_form_html, name, phone, remark;

        add_form_html = $(settings.input_add).html();
        $(settings.input_add).html("");

        // 显示楼层电话信息 隐藏 商场信息
        var showFloors = function (html) {
            $("#floors").html(html);
            $("#floors").show(settings.effects);
            $("#brands").hide(settings.effects);
            $("a.showMall").click(function () {
                $("#malls").show(settings.effects);
                $("#brands").hide(settings.effects);
            });
            $("a.mall_phone_list").click(function () {
                getObjVals(this);
                getList(mid, fid, bid);
            });
        }

        // 显示楼层品牌电话信息 隐藏 商场 楼层
        var showBrands = function (html) {
            $("#brands").html(html);
            $("#brands").show(settings.effects);
            $("#malls").hide(settings.effects);
        }

        var setAddAndUpdate = function () {
            // 执行添加显示
            $(".show_add").click(function () {
                createBox(add_form_html);
                getObjVals(this);
                $(settings.phone_do_add).click(function () {
                    getVals();
                    do_add(this, mid, fid, bid, name, phone, remark);
                });
            });
            // 更新号码
            $("a.update_phone").click(function () {
                var id = $(this).attr("pid");
                $.getJSON(setting.app + '/phone/update/' + id, function (data) {
                    if (data.status == 1) {
                        createBox(data.data);
                    } else {
                        createBox(data);
                    }
                });
            });
            // 删除号码
            $("a.delete_phone").click(
                function () {
                    if (window.confirm("确定删除？")) {
                        var id = $(this).attr("pid");
                        $.getJSON(setting.app + '/phone/delete/' + id,
                            function (data) {
                                if (data.status == 1) {
                                    $("#phone_" + id).hide(
                                        settings.effects);
                                } else {
                                    alert(data.data);
                                }
                            });
                    }
                });
        };

        // 获取号码列表
        var getList = function (mid, fid, bid) {
            $.getJSON(settings.url + '/list/' + mid + '/' + fid + '/' + bid,
                function (data) {
                    if (data.status) {
                        if (mid != 0 && fid != 0 && bid == 0) {
                            showBrands(data.data);
                        } else if (mid != 0 && fid == 0 && bid == 0) {
                            showFloors(data.data);
                        }
                        setAddAndUpdate();
                    } else {
                        alert(data.data);
                    }
                });
        };

        // 执行添加后的号码显示
        var setAddPhoneShow = function (html) {
            if (mid != 0 && fid != 0 && bid != 0) {
                $(html).insertAfter("#mall_" + mid + "_" + fid + "_" + bid);
            } else if (mid != 0 && fid != 0 && bid == 0) {
                $(html).insertAfter("#mall_" + mid + "_" + fid);
            } else if (mid != 0 && fid == 0 && bid == 0) {
                $(html).insertAfter("#mall_" + mid);
            }
        }

        // 添加号码
        var do_add = function (obj, mid, fid, bid, name, phone, remark) {
            $.getJSON(settings.url + '/add?mid=' + mid + '&fid=' + fid + '&bid=' + bid
                + "&name=" + name + "&phone=" + phone + "&remark=" + remark, function (data) {
                if (data.status) {
                    // 执行添加后显示
                    setAddPhoneShow(data.data);
                    setAddAndUpdate();
                    closeBox();
                } else {
                    alert(data.data);
                }
            });
        };
        // 获取应参数
        var getVals = function () {
            mid = $(settings.input_mid).val();
            fid = $(settings.input_fid).val();
            bid = $(settings.input_bid).val();
            name = $(settings.input_name).val();
            phone = $(settings.input_phone).val();
            remark = $(settings.input_remark).val();
        };

        // 获取当前变量中值 和 设置隐藏指
        var getObjVals = function (obj) {
            mid = $(obj).attr("mid");
            fid = $(obj).attr("fid");
            bid = $(obj).attr("bid");
            mid = mid ? mid : 0;
            fid = fid ? fid : 0;
            bid = bid ? bid : 0;
            $(settings.input_mid).val(mid);
            $(settings.input_fid).val(fid);
            $(settings.input_bid).val(bid);
        };
        // 执行列表显示
        $("a.phone_list").click(function () {
            getObjVals(this);
            getList(mid, fid, bid);
        });

        setAddAndUpdate();
    }
}(jQuery));